package org.itboys.mobile.service.mongo.mobile.cookbook;


import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.dto.cookbook.KitchenCookBookProductDto;
import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBookProduct;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.mobile.collection.SundayMobileCollectionService;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：zzq
 * 时间：2017年5月10日
 * 描述：花果山（农厨）_移动端_菜谱_食材_service
 */
@Service
public class KitchenMobileCookBookProductService extends BaseMongoService<KitchenCookBookProduct> {
    @Autowired
    private SundayProductMapper productMapper;


    @Resource(name = "kitchenDs")
    private MongoDataSource kitchenDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<KitchenCookBookProduct> getEntityClass() {
        return KitchenCookBookProduct.class;
    }

/**
     * 查询某个食谱所有食材。不分页
     * @param cookBookId
     * @return
     */

    public List<KitchenCookBookProductDto> selectBookProduct(Long cookBookId){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",0);
        param.put("cookBookId",cookBookId);
        return  packageDtoMuilt(super.list(param));
    }


/**
     * 组装多个实体
     * @param bookProducts
     * @return
     */

    /**
     * 组装DTO
     * @param bookProduct
     * @return
     */
    public KitchenCookBookProductDto packageDtoSingle(KitchenCookBookProduct bookProduct,SundayProduct product){
        Map<String,Object> param = new HashMap<String,Object>();
        if(product==null){
            product = productMapper.find(bookProduct.getProductId());
        }

        return new KitchenCookBookProductDto(product.getId(),product.getCreateTime(),product.getCreateDate(),product.getName(),
                product.getNumber(),product.getStandard(),bookProduct.getNum());
    }
    public List<KitchenCookBookProductDto> packageDtoMuilt(List<KitchenCookBookProduct> bookProducts) {
        Map<String, Object> param = new HashMap<String, Object>();
        List<KitchenCookBookProductDto> dtos = new ArrayList<KitchenCookBookProductDto>();
        List<Long> productIds = new ArrayList<Long>();

        for (KitchenCookBookProduct bookroduct : bookProducts) {
            productIds.add(bookroduct.getProductId());
        }
        if (ListUtil.isNotNull(productIds)) {
            param.put("ids", productIds);

            List<SundayProduct> products = new ArrayList<SundayProduct>();
            products = productMapper.select(param);
            for (KitchenCookBookProduct bookProduct : bookProducts) {
                for (SundayProduct product : products) {
                    if (LongUtil.isNotZreo(bookProduct.getProductId()) &&
                            bookProduct.getProductId().longValue() == product.getId().longValue()) {

                        dtos.add(packageDtoSingle(bookProduct, product));
                        break;
                    }
                }
            }


        }
        return dtos;
    }




}
